<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input
        v-model="params.carNumber"
        clearable
        placeholder="请输入内容"
        class="search-main"
        @keydown.enter="handleSearch"
      />
      <span class="search-label">车主姓名：</span>
      <el-input
        v-model="params.personName"
        clearable
        placeholder="请输入内容"
        class="search-main"
        @keydown.enter="handleSearch"
      />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatus">
        <el-option
          v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" class="search-btn" @click="handleSearch">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/parking/card/add')">添加月卡</el-button>
      <el-button
        :type="isSelectionMode ? 'danger' : 'default'"
        @click="toggleSelection"
      >{{ isSelectionMode ? (selectedCarList.length > 0 ? '删除' : '取消') : '批量删除' }}</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="cardList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          v-if="isSelectionMode"
          type="selection"
          width="55"
        />
        <el-table-column type="index" label="序号" />
        <el-table-column label="车主名称" prop="personName" header-align="center" align="center" />
        <el-table-column label="联系方式" prop="phoneNumber" header-align="center" align="center" />
        <el-table-column label="车牌号码" prop="carNumber" header-align="center" align="center" />
        <el-table-column label="车辆品牌" prop="carBrand" header-align="center" align="center" />
        <el-table-column label="状态" prop="cardStatus" header-align="center" align="center">
          <template #default="scope">
            {{ scope.row.cardStatus === 0 ? '可用' : '已过期' }}
          </template>
        </el-table-column>
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" align="center" />
        <el-table-column label="操作" fixed="right" width="180" header-align="center" align="center">
          <template #default="scope">
            <el-button
              size="mini"
              type="text"
              @click="$router.push({path: '/parking/card/add',query: {id: scope.row.id,type: 'renew'}
              })"
            >续费</el-button>
            <el-button size="mini" type="text">查看</el-button>
            <el-button size="mini" type="text" @click="editCard(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delCard(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          :current-page="params.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!-- 添加楼宇 -->
    <el-dialog
      title="添加楼宇"
      width="580px"
    >
      <div class="form-container">
        <!-- <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form> -->
      </div>
      <template #footer>
        <el-button size="mini">取 消</el-button>
        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getCardListAPI, delCardAPI } from '@/api/card'
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
        carNumber: '',
        personName: '',
        cardStatus: ''
      },
      isSelectionMode: false, // 是否处于选择模式
      selectedCarList: [],
      cardList: [],
      total: 0, // 总记录数
      statusOptions: [
        { value: '', label: '全部' },
        { value: 0, label: '可用' },
        { value: 1, label: '已过期' }
      ]
    }
  },
  mounted() {
    // 调用获取列表方法
    this.getCardList()
  },
  methods: {
    // 删除逻辑
    async delCard(id) {
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })

        const res = await delCardAPI(id)
        this.$message({
          type: res.code === 10000 ? 'success' : 'warning',
          message: res.msg
        })
        this.getCardList()
      } catch (error) {
        if (error !== 'cancel' && error.message) {
          this.$message.error(error.message)
        }
      }
    },
    // 批量删除方法修改
    async handleBatchDelete() {
      if (this.selectedCarList.length === 0) return
      try {
        await this.$confirm('此操作将永久删除选中的月卡信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })

        const ids = this.selectedCarList.map(item => item.id).join(',')
        const res = await delCardAPI(ids)
        console.log('删除测试：', res)

        this.$message({
          type: res.code === 10000 ? 'success' : 'warning',
          message: res.msg
        })

        this.isSelectionMode = false
        this.selectedCarList = []
        this.getCardList()
      } catch (error) {
        if (error !== 'cancel' && error.message) {
          this.$message.error(error.message)
        }
      }
    },
    // 获取数据列表
    async getCardList() {
      const res = await getCardListAPI(this.params)
      console.log(res)
      this.cardList = res.data.rows
      this.total = res.data.total // 设置总记录数
    },
    // 处理分页大小变化
    handleSizeChange(newSize) {
      this.params.pageSize = newSize
      this.getCardList()
    },
    // 处理当前页变化
    handleCurrentChange(newPage) {
      this.params.page = newPage
      this.getCardList()
    },
    // 表格多选
    handleSelectionChange(rowList) {
      this.selectedCarList = rowList
    },
    // 处理搜索
    handleSearch() {
      this.params.page = 1 // 重置页码
      this.getCardList()
    },
    // 编辑按钮
    editCard(id) {
      this.$router.push({
        path: '/parking/card/add',
        query: {
          id
        }
      })
    },
    // 切换选择模式
    toggleSelection() {
      if (this.isSelectionMode && this.selectedCarList.length > 0) {
        // 如果当前是选择模式且有选中项，则执行批量删除
        this.handleBatchDelete()
      } else {
        // 否则切换选择模式
        this.isSelectionMode = !this.isSelectionMode
        this.selectedCarList = [] // 清空选择
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn{
    margin-left:20px;
  }
}
.create-container{
  margin: 10px 0px;
}
.table {
  position: relative;
}
.pagination-container {
  position: absolute;
  right: 0;
  top: 100%;
  margin-top: 10px;
}
.form-container{
  padding:0px 80px;
}
</style>
